package com.pzb.web.controller.market;

import java.util.List;

import com.pzb.goods.domain.GdGoods;
import com.pzb.goods.service.IGdGoodsService;
import com.pzb.wechat.service.IWechatAppletService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pzb.common.annotation.Log;
import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.enums.BusinessType;
import com.pzb.market.domain.MkCoupon;
import com.pzb.market.service.IMkCouponService;
import com.pzb.common.core.page.TableDataInfo;

/**
 * 优惠券Controller
 * 
 * @author pzb
 * @date 2021-10-11
 */
@RestController
@RequestMapping("/market/coupon")
public class MkCouponController extends BaseController
{
    @Autowired
    private IMkCouponService mkCouponService;
    @Autowired
    private IGdGoodsService gdGoodsService;
    @Autowired
    private IWechatAppletService wechatAppletService;

    /**
     * 查询优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(MkCoupon mkCoupon)
    {
        mkCoupon.setExtAppid(wechatAppletService.getExtAppid());
        startPage();
        List<MkCoupon> list = mkCouponService.selectMkCouponList(mkCoupon);
        return getDataTable(list);
    }


    /**
     * 查询优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:list')")
    @GetMapping("/couponList")
    public AjaxResult couponList(MkCoupon mkCoupon)
    {
        mkCoupon.setExtAppid(wechatAppletService.getExtAppid());
        List<MkCoupon> list = mkCouponService.selectMkCouponList(mkCoupon);
        return AjaxResult.success(list);
    }

    /**
     * 查询优惠券商品表
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:list')")
    @GetMapping(value = "/getGoodsList")
    public TableDataInfo getGoodsList(MkCoupon mkCoupon)
    {
        startPage();
        List<GdGoods> list = gdGoodsService.selectGdGoodsListByCouponId(mkCoupon.getId());
        return getDataTable(list);
    }

    /**
     * 获取优惠券详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mkCouponService.selectMkCouponById(id));
    }

    /**
     * 新增优惠券
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:add')")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MkCoupon mkCoupon)
    {
        String extAppid = wechatAppletService.getExtAppid();
        if("1".equals(extAppid)){
            return AjaxResult.error("您还没有小程序!");
        }
        mkCoupon.setExtAppid(extAppid);
        return toAjax(mkCouponService.insertMkCoupon(mkCoupon));
    }

    /**
     * 修改优惠券
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MkCoupon mkCoupon)
    {
        return toAjax(mkCouponService.updateMkCoupon(mkCoupon));
    }

    /**
     * 删除优惠券
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:remove')")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mkCouponService.deleteMkCouponByIds(ids));
    }
    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody MkCoupon mkCoupon)
    {
        return toAjax(mkCouponService.updateMkCoupon(mkCoupon));
    }

    /**
     * 添加优惠券商品
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:add')")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping("/addCouponGoods")
    public AjaxResult addCouponGoods(@RequestBody MkCoupon mkCoupon)
    {
        return toAjax(mkCouponService.insertMkCouponGoods(mkCoupon));
    }

    /**
     * 删除优惠券商品
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:remove')")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/delCouponGoods/{ids}")
    public AjaxResult delCouponGoods(@PathVariable Long[] ids)
    {
        return toAjax(mkCouponService.deleteMkCouponGoodsByIds(ids));
    }
}
